package com.datoucai.controller;

import com.datoucai.param.BaseResult;
import com.datoucai.param.NotificationParam;
import com.datoucai.service.INotificationService;
import com.datoucai.service.dto.NotificationInfoDto;
import com.datoucai.service.dto.NotificationTotalInfoDto;
import com.datoucai.utils.BaseResultUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/notifications")
@Slf4j
@Api(tags = "消息通知模块")
public class NotificationController {

    @Autowired
    private INotificationService notificationService;

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public BaseResult<NotificationParam> getNotifications(@RequestParam("userId") Long userId) {
        log.info("查询-消息通知列表-入参:{}",userId);
        NotificationParam param = new NotificationParam();
        List<NotificationInfoDto> notificationList = notificationService.getNotificationList(userId);
        NotificationTotalInfoDto notificationTotalInfoDto = notificationService.getTotal(userId);

        param.setList(notificationList);
        param.setTotal(notificationTotalInfoDto.getTotal());
        param.setReadCount(notificationTotalInfoDto.getReadCount());
        param.setUnreadCount(notificationTotalInfoDto.getUnreadCount());
        log.info("查询-消息通知列表-出参:{}",param);

        return BaseResultUtils.generateSuccess(param);
    }
    @RequestMapping(value = "/{id}/read",method = RequestMethod.PUT)
    public BaseResult<Integer> read(@PathVariable  Long id) {
        log.info("查询-消息通知列表-入参:{}",id);
        int count = notificationService.markReadById(id);
        log.info("查询-消息通知列表-出参:{}",count);
        return BaseResultUtils.generateSuccess(count);
    }
    @RequestMapping(value = "/batch/read/{ids}",method = RequestMethod.PUT)
    public BaseResult<Integer> batchRead(@PathVariable  List<Long> ids) {
        log.info("已读-批量消息通知列表-入参:{}",ids);
        int count = notificationService.markReadByIds(ids);
        log.info("已读-批量消息通知列表-出参:{}",count);
        return BaseResultUtils.generateSuccess(count);
    }
}
